import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.TreeMap;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JViewport;

/**
 * 
 * @author Adam Dickinson and Terry 
 *
 */
public class MapFrame extends JFrame {

	private  MapConsole console;
	private Region region;
	private JViewport vport;
	
	/**
	 * Constructor for MapFrame 
	 */
	public MapFrame() {
		region = null; 
	}
	
	/**
	 * Main 
	 * @param args
	 */

	public static void main(String[] args) {
		
		try {
			MapFrame run = new MapFrame();
			run.setBounds(200, 200, 650, 400);
			run.setDefaultCloseOperation(EXIT_ON_CLOSE);
			BorderLayout layout = new BorderLayout(1,2);
			run.setJMenuBar(new MapMenu(run));
			run.setLayout(layout);
			run.setVisible(true);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 
	 * @param run
	 * @param file
	 */
	public void run(MapFrame run, String file)
	{
		// if cancel button selected 
		if(file !=null){
		
		region = new Region(file);
		region.loadFromFile();
		run.console = new MapConsole(region.getArea(), run); 
		run.add(run.console,BorderLayout.NORTH);
		
		vport = new JViewport();
		vport.setView(new ViewPanel(region,run.console, run));
		vport.setViewPosition(new java.awt.Point());
		
		run.add(vport,BorderLayout.CENTER);
		run.setVisible(true);
		
		}
	}
	
	/**
	 * 
	 */
	
	public void savepointstofile()
	{
		if(region !=null)
		{	
			try{
				FileWriter writer = new FileWriter("Point.txt"); 
				writer.write(region.arrayListToString());
				writer.write("finalend");
				writer.close();

				// clear the arraylist 
				region.clearList(); 
			}
			catch(Exception ex)
			{
				System.out.println(ex.getMessage());
			}
		}
	}
	
	/**
	 * This method is used to determine if they are any 
	 * unsaved points remaining. It also returns a zero if region not created 
	 * @return the size of the region arraylist
	 */
	public int getArrayListSize()
	{
		if(region !=null)
		{
			return this.region.getArrayListSize();
		}
		else
		{
			return 0;
		}
	}
	
	/**
	 * 
	 * @return a view port 
	 */
	
	public JViewport getViewport() {
		return vport;
	}
}
	
 
	
	
